package persistencia;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import negocio.Usuario;
import negocio.Supervisor;
import negocio.Administrativo;

public class SupervisorDAO {

	//TODO: falta agregar mas para que capa negocio se comunique con esta clase	de capa persistencia
    //TODO: refactorizar codigo para adecuarlo.

		private static SupervisorDAO instancia;
		private final Connection c;
		
		private SupervisorDAO()
		{
			c = BDManager.getInstance().getConexion();
		}

		public static SupervisorDAO getInstance()
		{
			if(instancia ==null)
			{
				instancia = new SupervisorDAO();
			}
			return instancia;
		}
		
		public void guardar(Supervisor usu)
		{
			String query = "INSERT INTO usuario( dni, nombre, apellido, telefono, mail, nickname, contrasenia) VALUES"+ " ('" + usu.getDni() + "','" + usu.getNombre() +"','" + usu.getApellido() + "','" + usu.getTelefono() +"','"+ usu.getMail()+ "','" + usu.getNickname() + "','" + usu.getContrasenia() +"')";
			
			try {
				Statement s = c.createStatement();
				s.execute(query);
				s.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
		
		
		public ArrayList<Supervisor> getAll()
		{
			String query = "SELECT * FROM curso";
			Statement s;
			ResultSet rs;
			ArrayList<Supervisor> res = new ArrayList<Supervisor>();
			try {
				s = c.createStatement();
				rs = s.executeQuery(query);
				while(rs.next())
				{
					res.add(new Supervisor(rs.getString("dni"), rs.getString("nombre"), rs.getString("apellido"), rs.getInt("telefono"), rs.getString("mail"),rs.getString("nickname"), rs.getString("contrasenia"), "foo"));
				}
				s.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			return res;
		}

		public Supervisor getSupervisor(int dni) 
		{
			String query = "SELECT * FROM supervisor WHERE dni=" + dni;
			Statement s;
			ResultSet rs;
			Supervisor res = null;
			try{
				s = c.createStatement();
				rs = s.executeQuery(query);
				rs.next();
				res = new Supervisor(rs.getString("dni"), rs.getString("nombre"), rs.getString("apellido"), rs.getInt("telefono"), rs.getString("mail"),rs.getString("nickname"), rs.getString("contrasenia"), "foo");
			}
			catch(SQLException e)
			{
				e.printStackTrace();
			}
			return res;
			
		}


		//SE devuelve Objeto, para luego comparar si permanece null la variable o no cuando se invoca este metodo.
		  public Supervisor buscarUser(String username, String password) throws SQLException {
		    String query = String.format(
		      "select * from supervisor where nickname = '%s' and contrasenia = '%s';",
		      username, password
		    );
		    Statement s = c.createStatement();
		    ResultSet rs = s.executeQuery(query);
		    
		    Supervisor dto = null;
		
		    if (rs.next()) {
		
		      dto = new Supervisor(rs.getString("dni"), rs.getString("nombre"), rs.getString("apellido"), rs.getInt("telefono"), rs.getString("mail"),rs.getString("nickname"), rs.getString("contrasenia"), "foo");
		    }
		    s.close();
		    return dto; //si no existe devuelve null
		  }
		
		
		
	}	
	

